package com.jinzhi.jzweb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzapi.domain.UserDO;
import com.jinzhi.jzweb.dao.JazGroupProdDao;
import com.jinzhi.jzweb.dao.JazGroupSpecsDao;
import com.jinzhi.jzweb.domain.*;
import com.jinzhi.jzweb.domain.vo.JazGroupOrderVo;
import com.jinzhi.jzweb.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jinzhi.jzweb.dao.JazGroupOrderDao;
import com.jinzhi.common.base.CoreServiceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <pre>
 * 团购订单表
 * </pre>
 * <small> 2024-05-11 13:54:20 | Long</small>
 */
@Service
public class JazGroupOrderServiceImpl extends CoreServiceImpl<JazGroupOrderDao, JazGroupOrderDO> implements JazGroupOrderService {
    @Autowired
    private JazSysUserService jazSysUserService;
    @Autowired
    private JazGroupProdDao jazGroupProdDao;
    @Autowired
    private JazGroupSpecsDao jazGroupSpecsDao;

    @Override
    public Result<Page<JazGroupOrderDO>> selGroupOrderList(String name, String mobile, int current, int size, String createTime, String endTime, String status) {
        Page<JazGroupOrderDO> page = new Page<>(current, size);
        List<JazGroupOrderDO> voList = new ArrayList<>();
        Wrapper<JazGroupOrderDO> wrapper = new EntityWrapper<>();

        //一个手机号或者一个名字会有多个订单
        if (StringUtils.isNotBlank(mobile)) {
            List<UserDO> user = jazSysUserService.selectList(new EntityWrapper<UserDO>().like("mobile", "%"+mobile+"%"));
            if (!user.isEmpty()) {
                List<Long> userIds = user.stream().map(UserDO::getId).collect(Collectors.toList());
                wrapper.in("user_id", userIds);
            } else {
                return Result.ok(new Page<>());
            }
        }
        if (StringUtils.isNotBlank(name)){
            List<UserDO> users = jazSysUserService.selectList(new EntityWrapper<UserDO>().like("username", "%"+name+"%"));
            if (!users.isEmpty()) {
                List<Long> userIds = users.stream().map(UserDO::getId).collect(Collectors.toList());
                wrapper.in("user_id", userIds);
            } else {
                return Result.ok(new Page<>());
            }
        }
        if (StringUtils.isNotBlank(createTime) && StringUtils.isNotBlank(endTime)) {
            wrapper.between("create_time", createTime, endTime);
        }
        //全部订单
        if (StringUtils.isNotBlank(status)&& !status.equals("-1")){
            wrapper.eq("order_status", status);
        }else if (status.equals("未支付")||status.equals("支付成功")||status.equals("用户已取消")){
            wrapper.eq("wx_status", status);
        }
        wrapper.ne("wx_status","未支付");
        wrapper.eq("order_pay_type",0);
        wrapper.orderBy("create_time",false);
        Page<JazGroupOrderDO> jazGroupOrderDOPage = this.selectPage(page, wrapper);
        for (JazGroupOrderDO jazGroupOrderDO : jazGroupOrderDOPage.getRecords()) {
            JazGroupOrderDO vo = new JazGroupOrderDO();
            BeanUtils.copyProperties(jazGroupOrderDO, vo);
            JazGroupProdDO jazGroupProdDO = jazGroupProdDao.selectById(jazGroupOrderDO.getGroupProdId());
            vo.setGroupProdName(jazGroupProdDO.getName());
            UserDO user = jazSysUserService.selectById(jazGroupOrderDO.getUserId());
            vo.setUserName(user.getUsername());
            vo.setPhone(user.getMobile());
            JazGroupSpecsDO jazGroupSpecsDO = jazGroupSpecsDao.selectById(jazGroupOrderDO.getGroupSpecsId());
            vo.setSpecsName(jazGroupSpecsDO.getName());
            voList.add(vo);
        }
        jazGroupOrderDOPage.setRecords(voList);
        return Result.ok(jazGroupOrderDOPage);
    }

    @Override
    public JazGroupOrderVo getInfo(Long id) {
        JazGroupOrderVo vo=new JazGroupOrderVo();
        JazGroupOrderDO jazGroupOrderDO = this.selectById(id);
        BeanUtils.copyProperties(jazGroupOrderDO,vo);
        JazGroupProdDO jazGroupProdDO = jazGroupProdDao.selectById(jazGroupOrderDO.getGroupProdId());
        vo.setGroupProdName(jazGroupProdDO.getName());
        UserDO user = jazSysUserService.selectById(jazGroupOrderDO.getUserId());
        vo.setUserName(user.getUsername());
        vo.setPhone(user.getMobile());
//        vo.setUserName(jazGroupOrderDO.getReceivingName());
//        vo.setPhone(jazGroupOrderDO.getPhone());
        JazGroupSpecsDO jazGroupSpecsDO = jazGroupSpecsDao.selectById(jazGroupOrderDO.getGroupSpecsId());
        vo.setSpecsName(jazGroupSpecsDO.getName());
        vo.setPrice(jazGroupSpecsDO.getPrice());
        return vo;
    }

    /**
     * 定时器调用：判断商品团购券是否过期
     */
    @Override
    public void setValid() {
        List<JazGroupOrderDO> orderList = this.baseMapper.selOrderListByStatus();
        if(orderList!=null && !orderList.isEmpty()){
            orderList.forEach(order->{
                order.setOrderStatus(4);
            });
            this.updateBatchById(orderList);
        }

    }
}
